package com.fingard.dsp.bank.directbank.bon01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.directbank.czb01.CZB01Header;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class BON01Transaction extends BON01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            //region 一：参数定义
            Calendar qryFromCldar = Calendar.getInstance();        //记录整个查询开始日期的日历
            Calendar qryToCldar = Calendar.getInstance();          //记录整个查询截止日期的日历
            Calendar curQryFrom = Calendar.getInstance();          //本次分月查询开始日期（日历）
            Calendar curQryTo = Calendar.getInstance();            //本次分月查询结算日期（日历）
            //endregion

            //region 二：参数值初始化
            qryFromCldar.setTime(p_req7516.getDateFrom());      //设置整个查询开始日期
            qryToCldar.setTime(p_req7516.getDateTo());          //设置整个查询截止日期
            curQryFrom.setTime(qryFromCldar.getTime());         //设置本次查询开始日期
            //endregion

            //region 三：按自然月，分月（页）查询
            while (curQryFrom.compareTo(qryToCldar) <= 0 && tmpRespCode.length() == 0) {

                //region【1】计算本次查询截止日期
                if (DateHelper.compareMonth(curQryFrom, qryToCldar) == -1) {
                    //开始日期 与 截止日期 为 跨月关系时，查本月信息
                    curQryTo.setTime(curQryFrom.getTime());
                    curQryTo.set(Calendar.DAY_OF_MONTH, curQryTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    //开始日期 与 截止日期 为 同月关系时，查全部信息
                    curQryTo.setTime(qryToCldar.getTime());
                }
                //endregion

                //region【2】组装查询报文
                XmlTextWriter xmlWriter = new XmlTextWriter();
                BON01Header bankHeader = new BON01Header();
                bankHeader.trCode = "200110";//交易代码
                bankHeader.reqSeqNo = p_req7516.reqHeader.transSeqID;

                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("acno", p_req7516.actNum);
                xmlWriter.writeElementString("cur_code", convertCurToBank(p_req7516.actCur));
                xmlWriter.writeElementString("start_date", Format.Date8Format.format(curQryFrom.getTime()));
                xmlWriter.writeElementString("end_date", Format.Date8Format.format(curQryTo.getTime()));
                xmlWriter.writeEndElement();//body
                xmlWriter.writeEndElement();//ap
                xmlWriter.writeEndDocument();
                //endregion

                //region【3】发送请求报文
                String[] retStrArr = sendToBank(xmlWriter.toXmlString());
                //endregion

                //region【4】解析响应报文
                Map<String, String> parseResultMap = parseRespMsg(retStrArr, retAts, p_req7516.actArea);
                tmpRespCode = parseResultMap.get("tmpRespCode");
                tmpRespInfo = parseResultMap.get("tmpRespInfo");
                //endregion

                //region 【5】增加月数，继续查询
                curQryFrom.setTime(curQryTo.getTime());
                curQryFrom.add(Calendar.DAY_OF_MONTH, 1);
                //endregion
            }
            //endregion
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespInfo(tmpRespInfo);
            retAts.writeRespCode(tmpRespCode);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        tmpReq7516.higherLevelAct = p_req7515.higherLevelAct;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
